package online.heycm.rbac.common.utils;

import java.util.regex.Pattern;

/**
 * 正则工具类
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/18 17:33
 */
public class RegUtil {

    public static final String REG_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    public static final String REG_PHONE = "^1[3-9]\\d{9}$";

    public static final String REG_ID_CARD_15 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$";

    public static final String REG_ID_CARD_18 = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

    public static final String REG_PASSWORD = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[~!@#$%^&*()_+\\-\\=\\[\\]{}|\\:;\"'<>,.?/]).{8,16}$";

    private RegUtil() {
    }

    public static boolean match(String regex, String content) {
        return Pattern.matches(regex, content);
    }

    public static boolean isPhone(String phone) {
        return match(REG_PHONE, phone);
    }

    public static boolean isEmail(String email) {
        return match(REG_EMAIL, email);
    }

    public static boolean isIdCard(String idCard) {
        return match(idCard.length() == 15 ? REG_ID_CARD_15 : REG_ID_CARD_18, idCard);
    }

    public static boolean isPassword(String password) {
        return match(REG_PASSWORD, password);
    }
}
